package cc.wsyw126.java.schedule;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by junpeng.yang on 16/11/7.
 */
public class ThreadPoolFactory {
    public static ExecutorService newCachedThreadPool() {
        ExecutorService executorService = Executors.newCachedThreadPool(r -> {
            Thread thread = new Thread(r);
            thread.setDaemon(true);
            thread.setPriority(Thread.NORM_PRIORITY);
            return thread;
        });
        return executorService;
    }


    public static ExecutorService newSingleThreadExecutor() {
        ExecutorService executorService = Executors.newSingleThreadExecutor(r -> {
            Thread thread = new Thread(r);
            thread.setDaemon(true);
            thread.setPriority(Thread.NORM_PRIORITY);
            return thread;
        });
        return executorService;
    }

    public static ExecutorService newScheduledThreadPool() {
        ExecutorService executorService = Executors.newScheduledThreadPool(100, r -> {
            Thread thread = new Thread(r);
            thread.setDaemon(true);
            thread.setPriority(Thread.NORM_PRIORITY);
            return thread;
        });
        return executorService;
    }

    public static ExecutorService newFixedThreadPool() {
        ExecutorService executorService = Executors.newFixedThreadPool(10, r -> {
            Thread thread = new Thread(r);
            thread.setDaemon(true);
            thread.setPriority(Thread.NORM_PRIORITY);
            return thread;
        });
        return executorService;
    }
}
